const axios = require('axios');
const path = require('path');
const url = 'https://sapi.16888.com/wxapp/wx4cface15c95f3a46/app.php';
const params = {
    mod: 'price',
    extra: 'indexStyle',
    letter: 'sz',
    order: 0,
    page: 1,
    pageSize: 60,
    brandId: 57328
};
class Car {
    constructor(
        name,
        id,
        url,
        brandId,
        maxMarketPrice,
        minMarketPrice,
        maxPrice,
        minPrice
    ) {
        this.name = name
        this.id = id
        this.brandId = brandId
        this.url = url
        this.maxMarketPrice = maxMarketPrice
        this.minMarketPrice = minMarketPrice
        this.maxPrice = maxPrice
        this.minPrice = minPrice
    }
}
const carListPath = path.join(__dirname, 'brand.json');
const carList = require(carListPath).data;
const headers = {
    Host: 'sapi.16888.com',
    Connection: 'keep-alive',
    'xweb_xhr': 1,
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/6.8.0(0x16080000) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF MacWechat/3.8.7(0x13080710) XWEB/1191',
    Accept: '*/*',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    Referer: 'https://servicewechat.com/wx4cface15c95f3a46/6/page-frame.html',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Content-Type': 'application/json'
};
// sleep函数，用于模拟延迟
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
// 主异步函数
(async function fetchCarData(carList) {
    try {
        const results = [];

        for (const car of carList) {
            try {
                // 发起请求
                const response = await axios.get(url, {
                    params: {
                        ...params,
                        brandId: car.id
                    },
                    headers
                });
                if (response.status === 200) {
                    const data = response.data.data;

                    data.list.forEach(carItem => {
                        const carModel = new Car(
                            carItem.styleName,
                            carItem.styleId,
                            carItem.img,
                            car.id,
                            carItem.maxMarketPrice,
                            carItem.minMarketPrice,
                            carItem.maxPrice,
                            carItem.minPrice
                        );
                        results.push(carModel);
                    })

                    console.log(data)
                } else {
                    console.warn(`Request failed with status code ${response.status} for ID: ${car.id}`);
                }
            } catch (error) {
                console.error(`Error fetching URL for ID: ${car.id}, ${error.message}`);
            }

            // 模拟延迟
            await sleep(500); // 假设每次请求之间延迟500毫秒
        }

        // 输出结果
        console.log(results);

        // 如果需要写入文件的操作可以在这里进行
        const json = { data: results };
        const fs = require('fs');
        fs.writeFileSync(path.join(__dirname, 'car.json'), JSON.stringify(json, null, 2));

    } catch (error) {
        console.error(`Overall error during fetch operations: ${error.message}`);
    }
})(carList); // 传入carList并立即执行这个异步函数
